<template>
    <div class="add-menu">
        <p>数量:</p>
        <input type="text" v-model="count" @input="handleIntegerInput" @blur="removeLeadingZeros" />
        <p>金额:</p>
        <input type="text" v-model="amount" @input="handeleInput" @blur="formatAmount" />
        <p>时间:{{ time }}</p>
        <p>备注</p>
        <input type="text" v-model="description" placeholder="描述" />
        <button @click="showDatePicker">修改时间</button>
        <button @click="addRecord">确认</button>
    </div>
    <DatePicker v-model="timestamp" :isShow="isShowDatePicker" @close="closeDatePicker" @confirm="setTime">
    </DatePicker>
</template>
<style>
.add-menu {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    background-color: white;

    border-color: #4CAF50;
    border-style: solid;
    border-radius: 5px;
    padding: 1rem;
    background-color: white;
}

.add-menu button {
    margin-top: 1rem;
    font-size: 1rem;
    width: 30vw;
    background-color: #4CAF50;
    border: none;
    border-radius: 5px;
    color: white;
}

p {
    white-space: nowrap;
    text-align: center;
    line-height: 50px;
    font-size: 21px;
}

input {
    width: 70vw;
    font-size: 1.5rem;
    margin: 0.5rem;
}
</style>
<script>
import DatePicker from './DatePicker.vue';

export default {
    data() {
        return {
            count: "0",
            amount: "0",
            time: "",
            timestamp: this.getCurrentTime(),
            isShowDatePicker: false,
            description: "",
        }
    },
    props: {
        userName: String
    },
    methods: {
        showDatePicker() {
            this.isShowDatePicker = true;
        },
        closeDatePicker() {
            this.isShowDatePicker = false;
        },
        setTimeStamp(timestamp) {
            this.timestamp = timestamp;
            this.setTime();
        },
        setTime() {
            console.log(this.timestamp);
            this.time = new Date(this.timestamp * 1000).toLocaleString();
        },
        getCurrentTime() {
            let date = new Date();
            return date.valueOf() / 1000;
        },
        handleIntegerInput(event) {
            // 只允许输入整数
            let inputValue = event.target.value;
            // 使用正则过滤非数字字符
            inputValue = inputValue.replace(/[^0-9]/g, "");
            this.count = inputValue;
        },
        removeLeadingZeros() {
            // 去掉前导零（比如 00123 -> 123）
            if (this.count) {
                this.count = this.count.replace(/^0+/, "") || "0"; // 保证空值时仍为 "0"
            }
        },
        handeleInput(event) {
            let inputValue = event.target.value;
            inputValue = inputValue.match(/^\d*(\.?\d{0,2})/g)[0] || "";
            this.amount = inputValue;
        },
        formatAmount() {
            // 对金额字段应用相同的格式化规则
            let formattedValue = this.amount.replace(/^0+(\d)/, "$1");
            if (formattedValue === "" || formattedValue === ".") {
                formattedValue = "0.00";
            } else {
                formattedValue = parseFloat(formattedValue).toFixed(2);
            }
            this.amount = formattedValue;
        },
        addRecord() {
            //console.log(this.timestamp);
            const data = {
                count: this.count,
                amount: this.amount,
                time: this.timestamp,
                description: this.description
            }
            this.$emit("close", "success");
            this.$emit("submit", data);
        }
    },
    components: {
        DatePicker
    },
    mounted() {
        this.setTime();
    },
}
</script>